import numpy as np
from Tools import metrics_result
from Tools import readbunchobj

from joblib import dump
from sklearn.preprocessing import StandardScaler
# 在文件顶部导入LGBMClassifier
from lightgbm import LGBMClassifier

# 导入训练集
trainpath = "../train_word_bag/tf_idf_space.dat"
train_set = readbunchobj(trainpath)

# 导入测试集
testpath = "../test_word_bag/test_tf_idf_space.dat"
test_set = readbunchobj(testpath)

# 输出单词矩阵的类型
print("标准化/归一化前的矩阵形状:")
print(np.shape(train_set.tdm))
print(np.shape(test_set.tdm))

# 对数据进行标准化或归一化
scaler = StandardScaler(with_mean=False)
train_set.tdm = scaler.fit_transform(train_set.tdm)
test_set.tdm = scaler.transform(test_set.tdm)


# LightGBM
print("\n开始训练LightGBM模型...")
lgbm_clf = LGBMClassifier(verbosity=-1, n_estimators=1000, learning_rate=0.1, n_jobs=-1, early_stopping_rounds = 50)

lgbm_clf.fit(train_set.tdm, train_set.label)
print("LightGBM模型训练完成。")

dump(lgbm_clf, 'LightGBM_model.joblib')

print("USE: LightGBM模型 预测分类结果")
lgbm_predicted = lgbm_clf.predict(test_set.tdm)
lgbm_total = len(lgbm_predicted)
print("结束")

print("\nLightGBM模型：")
metrics_result(test_set.label, lgbm_predicted, lgbm_total)
